/*
 * DCE400服务系统 -- 后端服务接口 -- 数据访问层 -- 工单数据访问
 * 包括：
 */
package dao

import (
	"errors"
	entity2 "gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/db"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/util"
	"go.uber.org/zap"
	"strings"
	"time"
)

//********************************************
//Func  ：查询数据库：工单列表分页查询。
//Method：
//Usage ：
//********************************************
func SearchProcess(opinionType entity2.EOpinionType, workOrderType entity2.EWorkOrderType) ([]*entity2.Process, error) {
	session := db.Session().Table("process")
	defer session.Close()

	if workOrderType >= entity2.EWorkOrderType_Counsel &&
		workOrderType <= entity2.EWorkOrderType_Complaint {
		session = session.And("work_order_types= ?", workOrderType)
	}

	processes := []*entity2.Process{}
	err := session.Find(&processes)
	if err != nil {
		return nil, err
	}
	return processes, nil
}

//********************************************
//Func  ：查询数据库：工单列表分页查询。
//Method：
//Usage ：
//********************************************
func QueryProcessList(page, pageSize int) (int64, []*entity2.Process, error) {
	offset := (page - 1) * pageSize
	session := db.Session().Table("process") //工单
	defer session.Close()
	if pageSize > 0 {
		session.Limit(pageSize, offset)
	}
	processes := []*entity2.Process{}

	total, err := session.FindAndCount(&processes)
	if err != nil {
		return 0, nil, err
	}
	return total, processes, nil
}

//********************************************
//Func  ：查询数据库：指定工单的详情
//Method：GET
//Usage ：
//********************************************
func QueryProcessDetail(Id int64) (*entity2.Process, error) {
	session := db.Session().Table("process") //.Where("id=?",operatorId).Get(&operator)
	defer session.Close()

	var process entity2.Process
	has, err := session.Where("process_id=?", Id).Get(&process)
	if err != nil || has == false {
		return nil, err
	}
	return &process, nil
}

//********************************************
//Func  ：添加新工单
//Method：POST
//Usage ：
//********************************************
func InsertProcess(process entity2.Process) (*entity2.Process, error) {
	var err error
	if len(strings.TrimSpace(process.ProcessName)) <= 0 { //姓名（必填）/单位名称
		return nil, errors.New("name is none")
	}

	process.CreatedAt = time.Now()
	process.ProcessId = util.GenSnowflakeInt64()
	global.Log.Info("InsertProcess: ", zap.Any("process:", process))

	session := db.Session().Table("process")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return nil, errors.New("db session begin failed.")
	}

	_, err = session.Insert(&process) //插入
	if err != nil {
		session.Rollback()
		return nil, err
	}
	err = session.Commit()
	if err != nil {
		return nil, err
	}

	return &process, nil
}

//********************************************
//Func  ：添加新工单
//Method：POST
//Usage ：
//********************************************
func UpdateProcess(process entity2.Process) (*entity2.Process, error) {
	var err error
	if len(strings.TrimSpace(process.ProcessName)) <= 0 { //姓名（必填）/单位名称
		return nil, errors.New("title is none")
	}

	if process.ProcessId <= 0 {
		return nil, errors.New("dept is lost")
	}
	global.Log.Info("UpdateProcess: ", zap.Any("step:", process))

	session := db.Session().Table("process")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return nil, errors.New("db session begin failed.")
	}

	_, err = session.Where("step_id=?", process.ProcessId).Update(&process) //插入
	if err != nil {
		session.Rollback()
		return nil, err
	}
	err = session.Commit()
	if err != nil {
		return nil, err
	}

	return &process, nil
}

//********************************************
//Func  ：Customer删除
//Method：DELETE
//Usage ：客户-账号删除--物理删
//********************************************
func DeleteProcess(stepId int64) error {

	session := db.Session().Table("process")
	defer session.Close()

	if stepId <= 0 {
		return errors.New("unknown stepid")

	}

	var step entity2.Process
	session.Begin()
	cnt, err := session.Where("id=?", stepId).Delete(&step)
	if err != nil {
		session.Rollback()
		return err
	}

	if cnt <= 0 {
		session.Rollback()
		return errors.New("移除步骤数量0")
	}
	err = session.Commit()
	return err
}
